package com.welsmann.app.start.spider.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.welsmann.app.start.spider.common.SpiderUtil;
import com.welsmann.app.start.spider.dao.NewsDAO;
import com.welsmann.app.start.spider.dao.NewsTitleDAO;
import com.welsmann.app.start.spider.dao.NewspiderDAO;
import com.welsmann.app.start.spider.entity.News;
import com.welsmann.app.start.spider.entity.NewsTitle;
import com.welsmann.app.start.spider.entity.Newspider;

/**
 * 新闻操作服务
 * 
 * @author Welsmann
 * 
 */
public class NewsService {

	private static SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static List<NewsTitle> getTop200NewsTitle() {
		return NewsTitleDAO.findTop200();
	}

	public static List<Newspider> getAllNewspider() {
		return NewspiderDAO.findAll();
	}

	public static void saveNews(String title, String time, String author,
			String content, String type, String link) {
		String uuid = SpiderUtil.uuid();
		NewsTitle nt = new NewsTitle(uuid);
		nt.setPublish(time);
		nt.setType(type);
		nt.setTitle(title);
		nt.setLink(link);
		nt.setSource(author);
		NewsTitleDAO.insert(nt);
		News n = new News(uuid);
		n.setContent(content);
		NewsDAO.insert(n);
	}

	public static Date getLatestPostDate(String typeId) {
		NewsTitle nt = NewsTitleDAO.getLatestPost(typeId);
		try {
			return nt == null || nt.getPublish() == null
					|| nt.getPublish().length() < 8 ? sdf
					.parse("2013-03-15 00:00:00") : sdf.parse(nt.getPublish());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

}
